package com.sskj.gcc.presenter;

import android.util.Log;

import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.DisposUtil;
import com.sskj.gocoin.m.bean.AppVersionBean;
import com.sskj.gcc.http.HttpConfig;
import com.sskj.gcc.ui.activity.MainActivity;
import com.sskj.level.bean.GonggaoBean;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.http.JsonCallBack;

import java.util.List;

import io.reactivex.disposables.Disposable;


public class MainActivityPresenter extends BasePresenter<MainActivity> {
    private WebSocketObserver stockSocket;
    private Disposable stockDisposable;

    public void getGonggao() {
        httpService.getGonggao()
                .execute(new JsonCallBack<HttpData<GonggaoBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<GonggaoBean>> response) {
                        HttpData<GonggaoBean> httpData = response.body();
                        if (httpData.getCode() == HttpConfig.OK) {
                            mView.showGonggao(httpData.getData());
                        }
                    }
                });
    }

    public void initWebSocket() {
        if (stockSocket == null) {

            httpService.getProduct("")
                    .execute(new JsonCallBack<HttpData<List<CoinBean>>>() {
                        @Override
                        public void onSuccess(Response<HttpData<List<CoinBean>>> response) {
                            HttpData<List<CoinBean>> httpData = response.body();
                            if (httpData.getCode() == HttpConfig.OK&&httpData.getData()!=null&&httpData.getData().size()>0) {
                                String code="";
                                for (CoinBean coinBean:httpData.getData()){
                                    code+=coinBean.getCode()+"|";
                                }
                                stockSocket = httpService.pushCoinType(code.substring(0,code.length()-1));
                                stockSocket.map(s -> new Gson().fromJson(s, CoinBean.class))
                                        .subscribe(coinBean ->
                                        {
                                            if (coinBean != null) {

                                                RxBus.getDefault().post(coinBean);
                                            }
                                        }, e -> {
                                            int i=0;
                                            Log.e("aaa","aaa");
                                        });
                            }
                        }
                    });



        } else {
//            stockSocket.send(new Gson().toJson(new SocketSendBean(Constans.NO_FIVE_PUSH)));
//            stockSocketFive.send(new Gson().toJson(new SocketSendBean(Constans.FIVE_PUSH)));
        }

    }


    public void closeWebSocket() {
        stockSocket = null;
        DisposUtil.close(stockDisposable);
    }

    public void getNewVersion() {
        httpService.getVersion()
                .execute(new JsonCallBack<HttpData<AppVersionBean>>(this,false,false) {
                    @Override
                    public void onSuccess(Response<HttpData<AppVersionBean>> response) {
                        HttpData<AppVersionBean> httpData = response.body();
                        if (httpData.getCode() == HttpConfig.OK) {
                            mView.onVersionDataSuccess(httpData.getData());
                        }
                    }

                    @Override
                    public void onError(Response<HttpData<AppVersionBean>> response) {
                        super.onError(response);
                        getGonggao();
                    }
                });
    }

}
